package com.wwwarehouse.contract.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.wwwarehouse.contract.bean.TerminationOrderBean;

import java.util.ArrayList;

import contract.wwwarehouse.com.contract.R;


public class TerminationOrderAdapter extends BaseAdapter {

    private ArrayList<TerminationOrderBean.ListBean> mData;
    private Context mContext;
    private ViewHolder mHolder;
    private ListView mListView;

    public TerminationOrderAdapter(ArrayList<TerminationOrderBean.ListBean> data, Context context, ListView listView) {
        this.mData = data;
        this.mContext = context;
        mListView = listView;
    }

    @Override
    public int getCount() {
        return mData == null ? 0 : mData.size();
    }

    @Override
    public Object getItem(int position) {
        return mData.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.contract_item_terminate_order, parent, false);
            mHolder = new ViewHolder(convertView);
            convertView.setTag(mHolder);
        } else {
            mHolder = (ViewHolder) convertView.getTag();
        }
        final View finalConvertView = convertView;
        mListView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                ViewGroup.LayoutParams params = finalConvertView.getLayoutParams();
                params.height = (int) ((mListView.getHeight() - 15) / 7);
                finalConvertView.setLayoutParams(params);
                mListView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
            }
        });
        mHolder.mOrderName.setText("终止方：" + mData.get(position).getStopName());
        mHolder.mOrderTime.setText("终止时间： " + mData.get(position).getStopTime());
        mHolder.mOrderNumber.setText("订单号：" + mData.get(position).getContractId());
        return convertView;
    }

    class ViewHolder {
        TextView mOrderName;
        TextView mOrderTime;
        TextView mOrderNumber;

        public ViewHolder(View view) {
            mOrderName = (TextView) view.findViewById(R.id.termination_name);
            mOrderTime = (TextView) view.findViewById(R.id.termination_time);
            mOrderNumber = (TextView) view.findViewById(R.id.termination_ordernumber);
        }
    }
}
